<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增分类</a-button>
      <a-button type="primary" preIcon="ic:round-expand" @click="expandAll">展开全部</a-button>
      <a-button type="primary" preIcon="ic:round-compress" @click="collapseAll">折叠全部</a-button>

      <a-dropdown v-if="checkedKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="i-ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button
          >批量操作
          <Icon icon="i-ant-design:down-outlined" />
        </a-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>

    <template #logo="{ record }">
      <ImagePreview :image-list="[{ src: record.logo, width: 60 }]" />
    </template>
  </BasicTable>
  <CategoryDrawer @register="registerDrawer" @success="handleSuccess" :showFooter="showFooter" />
</template>
<script lang="ts" name="shop-goods-category" setup>
  import { nextTick, ref } from "vue"
  import { ImagePreview } from "/@/components/Preview/index"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { Icon } from "/@/components/Icon"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { useDrawer } from "/@/components/Drawer"
  import CategoryDrawer from "./CategoryDrawer.vue"
  import { columns, searchFormSchema } from "./category.data"
  import { list, deleteCategory, batchDeleteCategory } from "./category.api"
  import { useRouter } from "vue-router"
  import { useMessage } from "/@/hooks/web/useMessage"

  const { createMessage } = useMessage()
  const router = useRouter()
  const checkedKeys = ref<Array<string | number>>([])
  const showFooter = ref(true)
  const [registerDrawer, { openDrawer }] = useDrawer()

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    tableProps: {
      title: "商品分类列表",
      api: list,
      immediate: true,
      formConfig: {
        showResetButton: false,
        schemas: searchFormSchema,
      },
      columns: columns,
      size: "small",

      pagination: false,
      isTreeTable: true,
      striped: true,
      useSearchForm: true,
      showTableSetting: false,
      bordered: true,
      showIndexColumn: false,
      tableSetting: { fullScreen: true },
      actionColumn: {
        width: 120,
      },
    },
  })
  //注册table数据
  const [registerTable, { reload, getForm, expandAll, collapseAll }] = tableContext

  /**
   * 选择列配置
   */
  const rowSelection = {
    type: "checkbox",
    columnWidth: 30,
    selectedRowKeys: checkedKeys,
    onChange: onSelectChange,
  }

  /**
   * 选择事件
   */
  function onSelectChange(selectedRowKeys: (string | number)[]) {
    checkedKeys.value = selectedRowKeys
  }

  /**
   * 新增
   */
  function handleCreate() {
    showFooter.value = true
    openDrawer(true, {
      isUpdate: false,
    })
  }

  /**
   * 编辑
   */
  function handleEdit(record) {
    showFooter.value = true
    openDrawer(true, {
      record,
      isUpdate: true,
    })
  }

  /**
   * 添加下级
   */
  function handleAddSub(record) {
    openDrawer(true, {
      record: { parentId: record.id, menuType: "1" },
      isUpdate: false,
    })
  }

  /**
   * 删除
   */
  async function handleDelete(record) {
    await deleteCategory({ id: record.id }, reload)
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteCategory({ ids: checkedKeys.value }, reload)
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    reload()
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: "删除",
        color: "error",
        popConfirm: {
          title: `是否确认删除 ${record.name}`,
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "添加下级",
        onClick: handleAddSub.bind(null, record),
      },
    ]
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return []
  }
</script>
